package cn.anquing.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author huangyong
 * @since 1.0.0
 */
public final class DateUtil {

    private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat YYYYMMDDH24MISSFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private DateUtil() {
    }

    /**
     * 获取当前日期与时间
     */
    public static String getCurrentDateTime() {
        return dateTimeFormat.format(new Date());
    }
    
    /**
     * 获取当前日期
     */
    public static String getCurrentDate() {
        return dateFormat.format(new Date());
    }
    
    /**
     * YYYYMMDDH24MISS eg:20080808080808 即:2008-08-08 08:08:08
     */
    public static String getYYYYMMDDH24MISSFormat(){
    	   return YYYYMMDDH24MISSFormat.format(new Date());
    }
    
/*    public static Date string2Date(String dateStr) throws ParseException{
    		java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    		Date date =  formatter.parse(dateStr);
			return date;
    }*/
    
    /**
     * compareDate,if dateStr before today,return ture.
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static boolean compareDate(String dateStr) throws ParseException{
    	Date nowdate=new Date(); 
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	Date d = sdf.parse(dateStr);
    	return d.before(nowdate);
    	
    	/*
    	boolean flag = d.before(nowdate);
    	if(flag)
    	System.out.print("早于今天");
    	else
    	System.out.print("晚于今天");
    	*/
    }
    
    /**
     * 转yyyy-MM-dd为MM月dd日
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static String convertFormat(String dateStr) throws ParseException{
    	Date nowdate=new Date(); 
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	SimpleDateFormat sdf2 = new SimpleDateFormat("MM月dd日");
    	Date d = sdf.parse(dateStr);
    		
    	return sdf2.format(d);
    	
    	/*
    	boolean flag = d.before(nowdate);
    	if(flag)
    	System.out.print("早于今天");
    	else
    	System.out.print("晚于今天");
    	*/
    }
    
    /**   
    * @Title: clacDate   
    * @Description:
    * @param bdate 输入日期
    * @return  输入日期与当前时间的相差的天数，大于0说明转入日期比当前时间大
    */ 
    public static int clacDate(String bdate){  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cal = Calendar.getInstance();    
        long time1 = cal.getTimeInMillis();                 
        try {
			cal.setTime(sdf.parse(bdate));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
        long time2 = cal.getTimeInMillis();         
        int days= (int)((time2-time1)/(1000*3600*24));  
       return days;
    }  
    
    /**
     *  指定日期与当前日期相差的月数
     * @param newDate    新的日期
     * @param oldDate    旧的日期
     * @return  如果d1>d2返回 月数差 否则返回0
     * @throws ParseException 
     */
    public static int getMonthDiff(String oldDate) {
    	   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        
        try {
			c2.setTime(sdf.parse(oldDate));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        if(c1.getTimeInMillis() < c2.getTimeInMillis()) return 0;
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值 假设 d1 = 2015-8-16  d2 = 2011-9-30
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if(month1 < month2 || month1 == month2 && day1 < day2) yearInterval --;
        // 获取月数差值
        int monthInterval =  (month1 + 12) - month2  ;
        if(day1 < day2) monthInterval --;
        monthInterval %= 12;
        return yearInterval * 12 + monthInterval;
    }
    
    
    /**
     * date2比date1多的天数
     * @param date1    
     * @param date2
     * @return    
     */
    public static int differentDays(String fromDate,String toDate)
    {
    	SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");    	
    	Date date1 = null;
    	Date date2 = null;
		try {
			date1 = format.parse(fromDate);
			date2= format.parse(toDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
       int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年            
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            
            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }
    
    public static void main(String[] args) {
		System.out.print(differentDays("2017-01-01","2017-03-04"));
	}
}
